//
//  RootTabViewController.m
//  Coding_My
//
//  Created by showXxie on 2017/8/21.
//  Copyright © 2017年 sooxie. All rights reserved.
//

#import "RootTabViewController.h"
#import "Project_RootViewController.h"
#import "MyTask_RootViewController.h"
#import "RDVTabBarItem.h"
#import "RKSwipeBetweenViewControllers.h"
#import "Tweet_RootViewController.h"
#import "Message_RootViewController.h"
@interface RootTabViewController ()<RDVTabBarControllerDelegate>
@end

@implementation RootTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewControllers];
}


- (void)setupViewControllers {

    Project_RootViewController *project = [[Project_RootViewController alloc] init];
    UINavigationController *nav_project = [[BaseNavigationController alloc] initWithRootViewController:project];

    MyTask_RootViewController *mytask = [[MyTask_RootViewController alloc] init];
    UINavigationController *nav_mytask = [[BaseNavigationController alloc] initWithRootViewController:mytask];
    
    
    RKSwipeBetweenViewControllers *nav_tweet = [RKSwipeBetweenViewControllers newSwipeBetweenViewControllers];

    
    [nav_tweet.viewControllerArray addObjectsFromArray:@[[Tweet_RootViewController newTweetVCWithType:Tweet_RootViewControllerTypeAll],
                                                         [Tweet_RootViewController newTweetVCWithType:Tweet_RootViewControllerTypeFriend],
                                                         [Tweet_RootViewController newTweetVCWithType:Tweet_RootViewControllerTypeHot]]];
    
    nav_tweet.buttonText = @[@"冒泡广场", @"朋友圈", @"热门冒泡"];

    
    
    Message_RootViewController *message = [[Message_RootViewController alloc] init];

    UINavigationController *nav_message = [[BaseNavigationController alloc] initWithRootViewController:message];

    
    [self setViewControllers:@[nav_project, nav_mytask, nav_tweet, nav_message, nav_project]];

    
    
    [self customizeTabBarForController];
    self.delegate = self;



}

- (void)customizeTabBarForController {
    
    UIImage *backgroundImage = [UIImage imageWithColor:kColorNavBG];
    NSArray *tabBarItemImages = @[@"project", @"task", @"tweet", @"privatemessage", @"me"];
    NSArray *tabBarItemTitles = @[@"项目", @"任务", @"冒泡", @"消息", @"我"];
    
//    NSArray *items = [[self tabBar] items];
//    for (int i = 0; i < items.count; i++) {
//        
//       RDVTabBarItem *item = (RDVTabBarItem *)items[i];
//        
//        item.titlePositionAdjustment = UIOffsetMake(0, 3);
//        
//        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
//                                                      [tabBarItemImages objectAtIndex:i]]];
//        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
//                                                        [tabBarItemImages objectAtIndex:i]]];
//        
//        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
//        
//        [item setTitle:[tabBarItemTitles objectAtIndex:i]];
//
//        if (i == 2) {
//            [item setY:-10];
//
//            [item setHeight:60];
//        }
//        
//    }
    
    
    NSInteger index = 0;

    for (RDVTabBarItem *item in [[self tabBar] items]) {
      
        item.titlePositionAdjustment = UIOffsetMake(0, 3);
        [item setBackgroundSelectedImage:backgroundImage withUnselectedImage:backgroundImage];
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabBarItemImages objectAtIndex:index]]];
        
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
      
        [item setTitle:[tabBarItemTitles objectAtIndex:index]];
       
        index++;
        

    }
    
    
//    [self.tabBar addLineUp:YES andDown:NO andColor:kColorCCC];

}

#pragma mark RDVTabBarControllerDelegate
- (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedViewController != viewController) {
        return YES;
    }
    if (![viewController isKindOfClass:[UINavigationController class]]) {
        return YES;
    }
    
    UINavigationController *nav = (UINavigationController *)viewController;
   
    if (nav.topViewController != nav.viewControllers[0]) {
        return YES;
    }
    if ([nav isKindOfClass:[RKSwipeBetweenViewControllers class]]) {
        
        RKSwipeBetweenViewControllers *swipeVC = (RKSwipeBetweenViewControllers *)nav;
        if ([[swipeVC curViewController] isKindOfClass:[BaseViewController class]]) {
            BaseViewController *rootVC = (BaseViewController *)[swipeVC curViewController];
            [rootVC tabBarItemClicked];
        }
    }else{
        if ([nav.topViewController isKindOfClass:[BaseViewController class]]) {
            BaseViewController *rootVC = (BaseViewController *)nav.topViewController;
            [rootVC tabBarItemClicked];
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
